package com.wyh.service.serviceImpl.gfiveImpl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyh.common.result.InvoicerecordsShowResult;
import com.wyh.common.result.Result;
import com.wyh.domain.po.gfive.Invoicedetails;
import com.wyh.domain.po.gfive.Invoicerecords;
import com.wyh.domain.po.gfive.PageHelper;
import com.wyh.domain.vo.gfive.TelAndEmailVO;
import com.wyh.mapper.gfivemapper.InvoicerecordsDTOMapper;
import com.wyh.service.gfive.InvoicerecordsService;
import com.wyh.mapper.gfivemapper.InvoicerecordsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author KmJHHhhhR
* @description 针对表【invoicerecords(开票记录表)】的数据库操作Service实现
* @createDate 2024-05-28 10:10:35
*/
@Service
public class InvoicerecordsServiceImpl extends ServiceImpl<InvoicerecordsMapper, Invoicerecords>
    implements InvoicerecordsService{

    @Autowired
    InvoicerecordsMapper invoicerecordsMapper;
    @Resource
    private InvoicerecordsDTOMapper InvoDTOMapper;
    @Resource
    JavaMailSender javaMailSender;
    @Value("${spring.mail.username}")
    private String from;
    @Override
    public List<InvoicerecordsShowResult> findAll(PageHelper pageHelper) {
        int currentPage = (pageHelper.getCurrentPage() - 1) * pageHelper.getPageSize();
        pageHelper.setCurrentPage(currentPage);
        return invoicerecordsMapper.findAll(pageHelper);
    }

    @Override
    public List<InvoicerecordsShowResult> findInvoicerecords(InvoicerecordsShowResult invoicerecordsShowResult, PageHelper pageHelper) {
        int currentPage = (pageHelper.getCurrentPage() - 1) * pageHelper.getPageSize();
        pageHelper.setCurrentPage(currentPage);
        return invoicerecordsMapper.findInvoicerecords(invoicerecordsShowResult,pageHelper);
    }

    @Override
    public TelAndEmailVO findTelAndEmail(Integer id) {
        return invoicerecordsMapper.findTelAndEmail(id);
    }

    @Override
    public Integer findCustomerId(Integer id) {
        return invoicerecordsMapper.findCustomerId(id);
    }

    @Override
    public Invoicerecords findOne(Integer id) {
        return invoicerecordsMapper.findOne(id);
    }

    @Override
    public List<Invoicedetails> seeInvoicedetails(String receiptnumber) {
        return invoicerecordsMapper.seeInvoicedetails(receiptnumber);
    }

    @Override
    public Result sendEmail(String telAndEmailVO,String receiptnumber) {
        List<Invoicedetails> invoicedetails = invoicerecordsMapper.seeInvoicedetails(receiptnumber);
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setFrom(from);  //发送人
        mailMessage.setTo(telAndEmailVO); //接收者
        mailMessage.setSubject("【票据推送信息】");
        List<String> urls = new ArrayList<>();
        for (int i = 0; i < invoicedetails.size(); i++) {
           urls.add(i,invoicedetails.get(i).getUrl());
        }
        String htmlContent = "<html><body><h2>票据内容</h2>";
        for (String url : urls) {
            htmlContent += "<img src='" + url + "'/><br>";
        }
        mailMessage.setText(htmlContent);
//        mailMessage.setText(invoicedetails.get(i).getUrl()); //内容
        mailMessage.setSentDate(new Date());
        javaMailSender.send(mailMessage);

        return Result.success();
    }

    @Override
    public Result moneyBack(Integer Invoicecode) {
        Invoicerecords invoicerecords = invoicerecordsMapper.findOne(Invoicecode);

        //获取账单金额 和账单id
        Double mount = invoicerecords.getAmount();
        Integer billid = invoicerecords.getBillid();

        //应交的金额
        Double aDouble = InvoDTOMapper.selecAmount(billid);
        if (aDouble == null){
            return Result.error("并无应交金额");
        }
        if (aDouble < mount) {
            return Result.error("数据异常，请查看具体数据在进行操作");
        }

        //进行回滚
        int i = InvoDTOMapper.UpdateMoey(mount, billid);
        if (i != 0) {
            return Result.success("回滚成功");
        }
        return Result.error("数据异常");
    }
}




